﻿using System;
using System.Collections.Generic;
using System.Linq;

using Fuse8.DomainFramework.Common.Locator;

using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.Domain.Commands;
using Fuse8.ContentManagementFramework.Domain.Commands.CommandParameters;

namespace Fuse8.ContentManagementFramework.BizFacade.Services
{
    public class CmsTemplateService : CmsSiteBusinessService<CmsTemplate>
    {
        public CmsTemplate DefaultTemplate
        {
            get
            {
                var allTemplates = FindAll();

                return allTemplates.FirstOrDefault(p => p.IsDefault) ?? allTemplates.First();
            }
        }

        public override IEnumerable<CmsTemplate> FindAll()
        {
            var command = DependencyServiceLocator.Current.GetInstance<CmsTemplateSelectAllCommand>();

            var commandParameter = new CmsTemplateSelectAllCommandParameter();

            SiteAwareEntityObjectMediator.SetSiteEntity(commandParameter);

            command.ConfigureCommand(commandParameter);

            return Find(command);
        }

        public override CmsTemplate FindOne(int id)
        {
            var command = DependencyServiceLocator.Current.GetInstance<CmsTemplateSelectOneCommand>();

            var commandParameter = new CmsTemplateSelectOneCommandParameter
            {
                TemplateId = id
            };

            SiteAwareEntityObjectMediator.SetSiteEntity(commandParameter);

            command.ConfigureCommand(commandParameter);

            return FindOne(command);
        }
    }
}
